package com.fenghuajueli.idiomppp.ui.idiomcategory;

import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ConvertUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.adapter.CategoryTabPagerAdapter;
import com.fenghuajueli.idiomppp.databinding.ActivityIdiomCategoryBinding;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.libbasecoreui.utils.ImageLoadManager;



public class IdiomCategoryActivity extends BaseActivity {


    RelativeLayout containerView;
    View ivIdiomAll;
    View ivIdiomStory;
    View ivYuyanStory;
    ViewPager viewPager;
    private int type = 0;//0表示选中成语大全  1表示成语故事   2表示寓言故事
    CategoryTabPagerAdapter categoryTabPagerAdapter;

    private ActivityIdiomCategoryBinding binding;


    private void initview() {
        containerView = binding.containerView;
        ivIdiomAll = binding.ivIdiomAll;
        ivIdiomStory = binding.ivIdiomStory;
        ivYuyanStory = binding.ivYuyanStory;
        viewPager = binding.viewPager;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityIdiomCategoryBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initview();
        OnClick(binding.getRoot());
        ImageLoadManager.loadForViewGroup(containerView, R.mipmap.aa_szxl_bj);
        type = getIntent().getIntExtra("type", 0);
        init();
    }


    private void init() {
        if (type == 0) {
            selectOne();
        } else if (type == 1) {
            selectTwo();
        } else {
            selectThree();
        }
        categoryTabPagerAdapter = new CategoryTabPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        viewPager.setAdapter(categoryTabPagerAdapter);
        viewPager.setOffscreenPageLimit(3);
        viewPager.setCurrentItem(type, false);
    }


    public void OnClick(View view) {
        int id = view.getId();
        if (id == R.id.ivIdiomAll) {
            selectOne();
        } else if (id == R.id.ivIdiomStory) {
            selectTwo();
        } else if (id == R.id.ivYuyanStory) {
            selectThree();
        }
    }

    private void selectOne() {
        viewPager.setCurrentItem(0, false);
        setSelected(ivIdiomAll, R.mipmap.aa_gushi_biaoti1_sel);
        setUnselect(ivIdiomStory, R.mipmap.aa_gushi_biaoti2_nor);
        setUnselect(ivYuyanStory, R.mipmap.aa_gushi_biaoti3_nor);
    }

    private void selectTwo() {
        viewPager.setCurrentItem(1, false);
        setSelected(ivIdiomStory, R.mipmap.aa_gushi_biaoti2_sel);
        setUnselect(ivIdiomAll, R.mipmap.aa_gushi_biaoti1_nor);
        setUnselect(ivYuyanStory, R.mipmap.aa_gushi_biaoti3_nor);
    }

    private void selectThree() {
        viewPager.setCurrentItem(2, false);
        setSelected(ivYuyanStory, R.mipmap.aa_gushi_biaoti3_sel);
        setUnselect(ivIdiomStory, R.mipmap.aa_gushi_biaoti2_nor);
        setUnselect(ivIdiomAll, R.mipmap.aa_gushi_biaoti1_nor);
    }

    private void setUnselect(View view, int resid) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        layoutParams.width = ConvertUtils.dp2px(64);
        layoutParams.height = ConvertUtils.dp2px(21);
        layoutParams.topMargin = ConvertUtils.dp2px(3);
        view.setLayoutParams(layoutParams);
        view.setBackgroundResource(resid);
    }


    private void setSelected(View view, int resid) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        layoutParams.width = ConvertUtils.dp2px(84);
        layoutParams.height = ConvertUtils.dp2px(32);
        layoutParams.topMargin = ConvertUtils.dp2px(0);
        view.setLayoutParams(layoutParams);
        view.setBackgroundResource(resid);
    }

}
